package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/10/21 12:32 AM
 * @description：https://leetcode.cn/problems/online-stock-span/
 */
public class 股票价格跨度 {
    @Test
    public void test() {
        StockSpanner stockSpanner = new StockSpanner();
        System.out.println(stockSpanner.next(28));
        System.out.println(stockSpanner.next(14));
        System.out.println(stockSpanner.next(28));
        System.out.println(stockSpanner.next(59));
        System.out.println(stockSpanner.next(79));
    }

    class StockSpanner {

        Stack<int[]> stack;
        int idx;
        public StockSpanner() {
            stack = new Stack<>();
            idx = -1;
            stack.push(new int[]{-1, Integer.MAX_VALUE});
        }

        public int next(int price) {
            idx++;
            while (price >= stack.peek()[1]) {
                stack.pop();
            }
            int res = idx - stack.peek()[0];
            stack.push(new int[]{idx, price});
            return res;
        }
    }

/**
 * Your StockSpanner object will be instantiated and called as such:
 * StockSpanner obj = new StockSpanner();
 * int param_1 = obj.next(price);
 */
}
